﻿using System;
using System.Collections.Generic;
using System.Security.Principal;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using HandyControl.Controls;
using HandyControl.Tools.Extension;
using WWLauncher.Constant;
using WWLauncher.Interface;
using WWLauncher.Util;
using WWLauncher.View.UserControls.Config;
using WWLauncher.ViewModel;

namespace WWLauncher.View
{
    /// <summary>
    /// ConfigDialog.xaml 的交互逻辑
    /// </summary>
    public partial class ConfigWindow : IWindowCommon, IChildrenWindow
    {
        private readonly List<UserControl> ucList = new();


        public ConfigWindow(AppConfig appConfig, MainWindow mainWindow)
        {
            RunTimeStatus.WpfWindowOpening = true;

            ucList.Add(new AboutControl());
            ucList.Add(new ThemeControl(this));
            ucList.Add(new MotionControl());
            ucList.Add(new OtherControl());
            ucList.Add(new AdvancedControl());
            ucList.Add(new DebugControl());

            InitializeComponent();
            this.DataContext = appConfig;
            RightCard.Content = ucList[1]; 
            listBox.SelectedIndex = 1;
            WindowUtil.SetOwner(this, mainWindow);

            UFG.Visibility = Visibility.Collapsed;
            UFG.Visibility = Visibility.Visible;

            this.ParentWindow = mainWindow;
            ucList.ForEach(uc => { ((IChildrenWindow)uc).ParentWindow = mainWindow; });

            bool hidden = true;
#if DEBUG
            hidden = false;
#endif
            if (hidden)
                SMDebug.Visibility = Visibility.Hidden;

            this.Loaded += ConfigWindow_Loaded;
        }
        public ConfigWindow(AppConfig appConfig, MainWindow mainWindow, int index): this(appConfig, mainWindow)
        {
            RightCard.Content = ucList[index];
            listBox.SelectedIndex = index;
        }

        private void ConfigWindow_Loaded(object sender, RoutedEventArgs e)
        {
            BGSettingUtil.BGSetting(BGBorder);
            Task.Run(() =>
            {
                //获得当前登录的Windows用户标示
                WindowsIdentity identity = WindowsIdentity.GetCurrent();
                WindowsPrincipal principal = new WindowsPrincipal(identity);
                //判断当前登录用户是否为管理员
                if (principal.IsInRole(WindowsBuiltInRole.Administrator))
                {
                    //添加环境变量
                    string pathStr = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine);
                    if (pathStr != null && !pathStr.Contains(Constants.AppDir)) // 修改系统环境变量Path值
                    {
                        Environment.SetEnvironmentVariable("Path", pathStr + ";" + Constants.AppDir + ";",
                            EnvironmentVariableTarget.Machine);
                    }
                }
            });
        }

        /// <summary>
        /// 点击关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


        /// <summary>
        /// 移动窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DragMove(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }


        public MainWindow ParentWindow { get; set; }



        public void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.DataContext = null;
                this.Close();
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            RunTimeStatus.WpfWindowOpening = false;
            ConfigUtil.SaveAppData();
        }

        private void ListBox_SelectionChanged(object sender,  EventArgs  e)
        {
            var listBox = (ListBox)sender;
            UFG.Visibility = Visibility.Collapsed;
            RightCard.Content = ucList[listBox.SelectedIndex];
            UFG.Visibility = Visibility.Visible;
        }
    }
}
